package cn.tedu.supermarket.controller;

import cn.tedu.supermarket.entity.*;
import cn.tedu.supermarket.mapper.AddressMapper;
import cn.tedu.supermarket.mapper.CartMapper;
import cn.tedu.supermarket.mapper.GoodsMapper;
import cn.tedu.supermarket.mapper.OrdersMapper;
import cn.tedu.supermarket.service.IOrdersService;
import cn.tedu.supermarket.service.SelectByStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class OrdersController {
    @Autowired
    IOrdersService ordersService;
    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    CartMapper cartMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    AddressMapper addressMapper;
    //用户方面
    //添加订单到订单数据库
    @RequestMapping("/addOrders")
    public String add(Integer arr[],String address, HttpSession session){
        if (session.getAttribute("user")!=null){
//            System.err.println(address);
            String[] add = address.split("\\s");
            int addressId = Integer.parseInt(add[0]);
//            System.err.println("addressId="+add[0]);
            for (int i = 0; i < arr.length; i++) {
//                System.err.println(arr[i]);
                int rows = ordersService.addOrders(arr[i],addressId);
//                System.err.println(rows);
                if (rows>0){
                    System.err.println("添加成功");
                    GoodsDTO goodsDTO = cartMapper.selectGoodsIdNumById(arr[i]);
                    goodsMapper.updateSumById(goodsDTO.getNum(),goodsDTO.getNum(),goodsDTO.getGoodsId());
                    cartMapper.deleteGoodsById(arr[i]);
                }
            }
            return "redirect:cartInfo";
        }
        return "buy_user/buserLogin";
    }
    //遍历该user的订单并显示
    @RequestMapping("/goOrders")
    public String order(HttpSession session, Model model){
        if (session.getAttribute("user")!=null){
            User user = (User) session.getAttribute("user");
            int[] arr = ordersMapper.selectAddressIdByUserId(user.getId());
            List<AddressDTO> address = new ArrayList<>();
            for (int i = 0; i < arr.length; i++) {
                AddressDTO addressDTO = addressMapper.selectById(arr[i]);
                System.err.println(arr[i]+","+addressDTO);
                address.add(addressDTO);
            }
            model.addAttribute("address",address);
            List<OrdersDTO> list = ordersMapper.selectByUserId(user.getId());
//            for (OrdersDTO ordersDTO : list) {
//                System.err.println(ordersDTO);
//            }
            model.addAttribute("list",list);
            return "buy_user/orders";
        }
        return "buy_user/buserLogin";
    }
    //订单已收货
    @RequestMapping("/changeStatus")
    public String changeStatus(int id){
        ordersMapper.updateStatusById(id);
        return "redirect:goOrders";
    }
    @Autowired
    SelectByStatus selectByStatus;
    //显示未收货订单
    @RequestMapping("/notReceived")
    public String notReceived(HttpSession session,Model model){
        String status = "未发货";
        if (session.getAttribute("user")!=null){
            User user = (User) session.getAttribute("user");
            selectByStatus.selectByStatus(user,model,status);
            return "buy_user/orders";
        }else {
            return "buy_user/buserLogin";
        }
    }
    //显示已发货订单
    @RequestMapping("/delivered")
    public String delivered(HttpSession session,Model model){
        String status = "已发货";
        model.addAttribute("status",status);
        if (session.getAttribute("user")!=null){
            User user = (User) session.getAttribute("user");
            selectByStatus.selectByStatus(user,model,status);
            return "buy_user/orders";
        }else {
            return "buy_user/buserLogin";
        }
    }
    //显示已收货订单
    @RequestMapping("/Received")
    public String Received(HttpSession session,Model model){
        String status = "已收货";
        if (session.getAttribute("user")!=null){
            User user = (User) session.getAttribute("user");
            selectByStatus.selectByStatus(user,model,status);
            return "buy_user/orders";
        }else {
            return "buy_user/buserLogin";
        }
    }

    //商家方面
    //遍历该user的订单并显示
    @RequestMapping("/selOrders")
    public String sel_order(HttpSession session, Model model){
        if (session.getAttribute("user")!=null){
            User user = (User) session.getAttribute("user");
            int[] arr = ordersMapper.selectAddressIdByUserId(user.getId());
            List<AddressDTO> add = new ArrayList<>();
            for (int i = 0; i < arr.length; i++) {
                AddressDTO addressDTO = addressMapper.selectById(arr[i]);
                add.add(addressDTO);
            }
            model.addAttribute("add",add);
            List<OrdersDTO> list1 = ordersMapper.selectByUserId(user.getId());
            model.addAttribute("list1",list1);
            return "sel_user/orders";
        }
        return "sel_user/suserLogin";
    }
    //订单已发货
    @RequestMapping("/selChangeStatus")
    public String delivered(int id){
        ordersMapper.updateStatus(id);
        System.err.println("修改成功");
        return "redirect:selOrders";
    }


}
